﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;

namespace CooStationsToolsNew
{
    /// <summary>
    /// NewDivWin.xaml 的交互逻辑
    /// </summary>
    public partial class NewDivWin : Window
    {
        public int thex,they,thealpha;
        public string thename = null;
        Popup popName = null;
        Popup popX = null;
        Popup popY = null;
        public NewDivWin()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            alphaSl.Value = 1;
            alphaSl.Maximum = 1;
            alphaSl.Minimum = 0;
            alphaSl.LargeChange = 0.1;
            alphaSl.SmallChange = 0.1;
            divx_txt.Text = "0";
            divy_txt.Text = "0";
            popName = setPopup("不能以数字开头!", divname_txt);
        }
        /// <summary>
        /// 提示框
        /// </summary>
        /// <param name="str">提示文字</param>
        /// <param name="uie">需要提示的对象</param>
        /// <returns></returns>
        public Popup setPopup(string str, UIElement uie)
        {
            Popup pop = new Popup();
            TextBlock tb = new TextBlock();
            ImageBrush ib = new ImageBrush();
            ib.ImageSource = new BitmapImage(new Uri("pic/popo.png", UriKind.RelativeOrAbsolute));
            ib.Stretch = Stretch.Fill;
            tb.Background = ib;
            tb.Width = 157.0;
            tb.Height = 62.0;
            tb.TextAlignment = TextAlignment.Center;
            tb.Padding = new Thickness(5.0);
            tb.Text = str;
            pop.Width = 157.0;
            pop.Height = 62.0;
            pop.Child = tb;
            pop.AllowsTransparency = true;
            pop.StaysOpen = false;
            pop.PlacementTarget = uie;
            pop.Placement = PlacementMode.Top;
            pop.PopupAnimation = PopupAnimation.Fade;
            return pop;
        }

        private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void lbtn_close_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        private void NewDivBt_Click(object sender, RoutedEventArgs e)
        {
            short num;
            if (divname_txt.Text == "")
            {
                popName = setPopup("名称不能为空", divname_txt);
                popName.IsOpen = true;
                divname_txt.Focus();
            }
            else if (divx_txt.Text == "")
            {
                popX = setPopup("X坐标不能为空", divx_txt);
                popX.IsOpen = true;
                divx_txt.Focus();
            }
            else if (!Int16.TryParse(divx_txt.Text,out num))
            {
                popX = setPopup("X坐标超出范围", divx_txt);
                popX.IsOpen = true;
                divx_txt.Focus();
            }
            else if (divy_txt.Text == "")
            {
                popY = setPopup("Y坐标不能为空", divy_txt);
                popY.IsOpen = true;
                divy_txt.Focus();
            }
            
            else if (!Int16.TryParse(divy_txt.Text, out num))
            {
                popY = setPopup("Y坐标超出范围", divy_txt);
                popY.IsOpen = true;
                divy_txt.Focus();
            }
            else if (ProjectTools.CheckId(divname_txt.Text, App.dt))
            {
                popName = setPopup("名称已存在！请重新输入", divname_txt);
                popName.IsOpen = true;
                divname_txt.Focus();
            }
            else
            {
                thex = int.Parse(divx_txt.Text);
                they = int.Parse(divy_txt.Text);
                thealpha = int.Parse(divalpha_txt.Text);
                thename = divname_txt.Text;
                this.DialogResult = true;
            }
        }

        private void NewDivCancelBt_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        private void alphaSl_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            divalpha_txt.Text = (Convert.ToInt32(e.NewValue * 100)).ToString();  
        }

        private void alphaTb_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                alphaSl.Value = double.Parse(divalpha_txt.Text) / 100;
            }
            catch { }
        }

      

        private void divname_txt_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            // System.Windows.MessageBox.Show(e.Key.ToString());
            System.Windows.Controls.TextBox txt = sender as System.Windows.Controls.TextBox;
            //屏蔽非法按键
            if ((e.Key < Key.A || e.Key > Key.Z) && txt.Text == "")
            {
                e.Handled = true;
                popName.IsOpen = true;
            }
            else if (e.Key == Key.Oem5 || e.Key == Key.OemQuestion || e.Key == Key.Divide || e.Key == Key.Multiply)
            {
                e.Handled = true;
                popName.IsOpen = true;
            }
            else if ((e.Key == Key.D8 || e.Key == Key.OemPeriod ||
                    e.Key == Key.Oem5 || e.Key == Key.Oem1 || e.Key == Key.Oem7 ||
                    e.Key == Key.OemQuestion || e.Key == Key.OemComma) && e.KeyboardDevice.Modifiers == ModifierKeys.Shift)
            {
                e.Handled = true;
                popName.IsOpen = true;
            }
        }

        private void divx_txt_PreviewKeyDown(object sender, KeyEventArgs e)
        { 

            if (e.Key == Key.Enter)
            {
                ((System.Windows.Controls.TextBox)sender).Focusable = false;
            }
            if (e.Key != Key.D0 && e.Key != Key.D1 && e.Key != Key.D2
                && e.Key != Key.D3 && e.Key != Key.D4 && e.Key != Key.D5
                && e.Key != Key.D6 && e.Key != Key.D7 && e.Key != Key.D8
                && e.Key != Key.D9 && e.Key != Key.NumPad0 && e.Key != Key.NumPad0
                && e.Key != Key.NumPad1 && e.Key != Key.NumPad2 && e.Key != Key.NumPad3
                && e.Key != Key.NumPad4 && e.Key != Key.NumPad5 && e.Key != Key.NumPad6
                && e.Key != Key.NumPad7 && e.Key != Key.NumPad8 && e.Key != Key.NumPad9
                && e.Key != Key.Back && e.Key != Key.LeftShift && e.Key != Key.Left && e.Key != Key.Right && e.Key != Key.Subtract && e.Key != Key.OemMinus)
            {
                e.Handled = true;
                popX = setPopup("只能输入数字", divx_txt);
                popX.IsOpen = true;
            }
            if (e.Key == Key.OemMinus && e.KeyboardDevice.Modifiers == ModifierKeys.Shift || e.Key == Key.OemMinus && e.KeyboardDevice.Modifiers == ModifierKeys.Shift && divx_txt.Text.Length >= 1)
            {
                popX = setPopup("只能输入数字", divx_txt);
                e.Handled = true;
                popX.IsOpen = true;
            }
            if (e.Key == Key.OemMinus || e.Key == Key.Subtract && divx_txt.Text.Length >= 1)
            {
                popX = setPopup("只能输入数字", divx_txt);
                e.Handled = true;
                popX.IsOpen = true;
            }
            if (e.Key == Key.D0 && e.KeyboardDevice.Modifiers == ModifierKeys.Shift || e.Key == Key.D1 && e.KeyboardDevice.Modifiers == ModifierKeys.Shift || e.Key == Key.D2 && e.KeyboardDevice.Modifiers == ModifierKeys.Shift
                || e.Key == Key.D3 && e.KeyboardDevice.Modifiers == ModifierKeys.Shift || e.Key == Key.D4 && e.KeyboardDevice.Modifiers == ModifierKeys.Shift || e.Key == Key.D5 && e.KeyboardDevice.Modifiers == ModifierKeys.Shift
                || e.Key == Key.D6 && e.KeyboardDevice.Modifiers == ModifierKeys.Shift || e.Key == Key.D7 && e.KeyboardDevice.Modifiers == ModifierKeys.Shift || e.Key == Key.D8 && e.KeyboardDevice.Modifiers == ModifierKeys.Shift
                || e.Key == Key.D9 && e.KeyboardDevice.Modifiers == ModifierKeys.Shift)
            {
                popX = setPopup("只能输入数字", divx_txt);
                e.Handled = true;
                popX.IsOpen = true;
            }
           
        }

        private void divname_txt_TextChanged(object sender, TextChangedEventArgs e)
        {
          
        }     
    }
}
